﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;


namespace Synchros
{
    public partial class FrmSystem : Form
    {
        public FrmSystem()
        {
            InitializeComponent();
        }

        private void FrmSystem_Load(object sender, EventArgs e)
        {
            string strParameter = LoadConfig();
            if (strParameter != null)
            {
                chkArun.Checked = true;
                if (strParameter.IndexOf("sync=1") > -1)
                {
                    chkSync.Checked = true;
                }
                if (strParameter.IndexOf("mini=1") > -1)
                {
                    chkMini.Checked = true;
                }
            }
            else
            {
                chkArun.Checked = false;
            }
        }

        private void chkArun_CheckedChanged(object sender, EventArgs e)
        {
            chkSync.Enabled = chkArun.Checked;
            chkMini.Enabled = chkArun.Checked;
            ConfigAutoRun();
        }
        private void chkSync_CheckedChanged(object sender, EventArgs e)
        {
            ConfigAutoRun();
        }
        private void chkMini_CheckedChanged(object sender, EventArgs e)
        {
            ConfigAutoRun();
        }
        private readonly string strKeyName = "Synchros";
        private readonly string strSubKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";//
        private string strSysPath = System.Windows.Forms.Application.StartupPath + "\\Synchros.exe";

        //配置 自动运行
        private bool ConfigAutoRun()
        {
            RegistryKey runKey = null;
            try
            {
                runKey = Registry.LocalMachine.OpenSubKey(strSubKey, true);
                if (runKey == null)
                {
                    runKey = Registry.LocalMachine.CreateSubKey(strSubKey);//software\\microsoft\\windows\\currentVersion\\run
                }
                string strParameter = string.Empty;
                strParameter += chkSync.Checked ? " sync=1" : " sync=0";
                strParameter += chkMini.Checked ? " mini=1" : " mini=0";
                runKey.SetValue(strKeyName, strSysPath + strParameter);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (runKey != null)
                {
                    runKey.Close();
                }
            }
            return true;
        }

        //取消 自动运行
        private bool CancelAutoRun()
        {
            RegistryKey runKey = null;
            try
            {
                runKey = Registry.LocalMachine.OpenSubKey(strSubKey, true);
                if (runKey != null)
                {
                    runKey.DeleteValue(strKeyName, false);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (runKey != null)
                {
                    runKey.Close();
                }
            }
            return true;
        }

        //获取配置信息
        private string LoadConfig()
        {
            RegistryKey runKey = null;
            try
            {
                runKey = Registry.LocalMachine.OpenSubKey(strSubKey, true);
                if (runKey != null)
                {
                    object objFilePath = runKey.GetValue(strKeyName);
                    if (objFilePath != null)
                    {
                        return objFilePath.ToString().Replace(strSysPath, "");//移除路径信息
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (runKey != null)
                {
                    runKey.Close();
                }
            }
            return null;
        }                
    }
}
